package com.roadjava.GDOUGym.service.impl;

import com.roadjava.GDOUGym.bean.dto.DenueHireMessageDTO;
import com.roadjava.GDOUGym.bean.dto.PlaceDTO;
import com.roadjava.GDOUGym.bean.entity.DenueHireMessageDO;
import com.roadjava.GDOUGym.bean.entity.EquipmentRentDO;
import com.roadjava.GDOUGym.bean.entity.PlaceDO;
import com.roadjava.GDOUGym.bean.res.ResultDTO;
import com.roadjava.GDOUGym.convert.DenueHireMessageConvert;
import com.roadjava.GDOUGym.convert.SiteConvert;
import com.roadjava.GDOUGym.mapper.DenueHireMessageMapper;
import com.roadjava.GDOUGym.mapper.SiteMapper;
import com.roadjava.GDOUGym.service.DenueHireMessageService;
import com.sun.xml.internal.bind.v2.TODO;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.swing.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;


@Service
@Slf4j
public class DenueHireMessageServiceImpl implements DenueHireMessageService {
    @Resource
    private DenueHireMessageMapper denueHireMessageMapper;

    @Resource
    private SiteMapper siteMapper;

    @Resource
    private DenueHireMessageConvert denueHireMessageConvert;

    @Override
    public ResultDTO<List<DenueHireMessageDTO>> find(DenueHireMessageDTO denueHireMessageDTO) {

        //预约场地信息
        List<DenueHireMessageDO> dtosList = denueHireMessageMapper.find(denueHireMessageDTO);
        for(int i = 0;i< dtosList.size();i++){
            System.out.println(dtosList.get(i).getPlaceNo());
            PlaceDTO placeDTO = new PlaceDTO();
            placeDTO.setPlaceNo(dtosList.get(i).getPlaceNo());
            List<PlaceDO> placeDOList = siteMapper.findSite(placeDTO);
            dtosList.get(i).setPlaceName(placeDOList.get(0).getPlaceName());
        }

        System.out.println(dtosList);
        List<DenueHireMessageDTO> dtos = denueHireMessageConvert.dos2dtos(dtosList);

        long total = denueHireMessageMapper.selectCount(denueHireMessageDTO);
        return ResultDTO.buildSuccess(dtos,total);
    }

    @Override
    public ResultDTO<List<DenueHireMessageDTO>> findCanApp(DenueHireMessageDTO denueHireMessageDTO) {
        //创建返回对象
        List<DenueHireMessageDO> denueHireMessageDOList = new ArrayList<>();

        //预约场地信息
        List<PlaceDO> dos = denueHireMessageMapper.findCanApp(denueHireMessageDTO);
        System.out.println("dos = " + dos);

        for(int i=0;i<dos.size();i++){
            DenueHireMessageDO dDO = new DenueHireMessageDO();
            dDO.setPlaceNo(dos.get(i).getPlaceNo());//场地号
            dDO.setPlaceName(dos.get(i).getPlaceName());//场地名
            dDO.setOrderCost(dos.get(i).getPlaceCost());//租用费用
            dDO.setOrderData(denueHireMessageDTO.getOrderData());//租用日期
            dDO.setOrderTimeSub(denueHireMessageDTO.getOrderTimeSub());//租用时间段
            dDO.setPlaceAddress(dos.get(i).getPlaceAddress());//场地详细位置
            dDO.setStaffID(dos.get(i).getStaffID());//场地负责人
            dDO.setPlacePrincipal(dos.get(i).getPlacePrincipal());//场地负责人电话
            denueHireMessageDOList.add(dDO);
        }
        List<DenueHireMessageDTO> dtos = denueHireMessageConvert.dos2dtos(denueHireMessageDOList);
        long total = denueHireMessageMapper.selectCanAppCount(denueHireMessageDTO);
        return ResultDTO.buildSuccess(dtos,total);
    }

    @Override
    public ResultDTO<String> add(DenueHireMessageDTO denueHireMessageDTO) {
        DenueHireMessageDTO dto = new DenueHireMessageDTO();


        String no;
        List<DenueHireMessageDO> denueHireMessageDOList = denueHireMessageMapper.find(dto);
        if(denueHireMessageDOList.size() == 0){
            no = "0001";
        }else{
            String endNo = denueHireMessageDOList.get(denueHireMessageDOList.size()-1).getOrderNo();
            long total = Integer.parseInt(endNo)+1;

            no = total+"";
            if(total<10){
                no = "000" + no;
            }else if(total<100){
                no = "00" + no;
            }else if(total < 1000){
                no = "0" + no;
            }
        }

        // 把dto转为do对象
        DenueHireMessageDO denueHireMessageDO = denueHireMessageConvert.dto2do(denueHireMessageDTO);
        denueHireMessageDO.setOrderNo(no);
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        denueHireMessageDO.setOrderTime(formatter.format(date));
        int result = denueHireMessageMapper.add(denueHireMessageDO);
        if (result == 1) {
            return ResultDTO.buildSuccess("预约成功");
        }else {
            return ResultDTO.buildSuccess("预约失败");
        }
    }

    @Override
    public ResultDTO<String> change(DenueHireMessageDTO denueHireMessageDTO) {
        // 把dto转为do对象
        DenueHireMessageDO denueHireMessageDO = denueHireMessageConvert.dto2do(denueHireMessageDTO);
        int result = denueHireMessageMapper.change(denueHireMessageDO);
        if (result == 1) {
            return ResultDTO.buildSuccess("修改成功");
        }else {
            return ResultDTO.buildSuccess("修改失败");
        }
    }

    @Override
    public ResultDTO<String> delete(DenueHireMessageDTO denueHireMessageDTO) {
        // 把dto转为do对象
        DenueHireMessageDO denueHireMessageDO = denueHireMessageConvert.dto2do(denueHireMessageDTO);
        int result = denueHireMessageMapper.delete(denueHireMessageDO);
        if (result == 1) {
            return ResultDTO.buildSuccess("退选成功");
        }else {
            return ResultDTO.buildSuccess("退选失败");
        }
    }
}
